//
//  AdoptedCell.m
//  PigMarket
//
//  Created by mac on 2019/5/6.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AdoptedCell.h"

@interface AdoptedCell ()
/**
 cell白色背景
 */
@property (nonatomic , strong) UIView *bgView;
/**
 unableBgView
 */
@property (nonatomic , strong) UIView *unableBgView;
/**
 头像
 */
@property (nonatomic , strong) UIImageView *headImg;
/**
 title
 */
@property (nonatomic , strong) UILabel *title;
/**
 状态  欠费 快到期
 */
@property (nonatomic , strong) UIImageView *pigStatusImg;
/**
 编号
 */
@property (nonatomic , strong) UILabel *pigNum;
/**
 欠费金额
 */
@property (nonatomic , strong) YYLabel *arrearageMoney;
/**
 托管到期
 */
@property (nonatomic , strong) UIButton *expiredTime;

/**
 z地址
 */
@property (nonatomic , strong) UIButton *address;

/**
 transfer
 */
@property (nonatomic , strong) UIButton *transferBtn;
/**
 look
 */
@property (nonatomic , strong) UIButton *lookBtn;
/**
 pay
 */
@property (nonatomic , strong) UIButton *payBtn;
/**
 横线
 */
@property (nonatomic , strong) UIImageView *lineImg;
/**
 竖线
 */
@property (nonatomic , strong) UIView *vLine1;
@property (nonatomic , strong) UIView *vLine2;
@end
@implementation AdoptedCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    AdoptedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[AdoptedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.headImg];
    [self.bgView addSubview:self.title];
    [self.bgView addSubview:self.pigStatusImg];
    [self.bgView addSubview:self.pigNum];
    [self.bgView addSubview:self.arrearageMoney];
    [self.bgView addSubview:self.expiredTime];
    [self.bgView addSubview:self.address];
    [self.bgView addSubview:self.lineImg];
    [self.bgView addSubview:self.lookBtn];
    [self.bgView addSubview:self.transferBtn];
    [self.bgView addSubview:self.payBtn];
    [self.bgView addSubview:self.vLine1];
    [self.bgView addSubview:self.vLine2];
    [self.bgView addSubview:self.unableBgView];
}

- (void)initLayout {
    // 布局子控件
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-15);
    }];
    [self.headImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(18);
        make.top.mas_equalTo(18);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(22);
        make.left.mas_equalTo(self.headImg.mas_right).offset(12);
        make.height.mas_equalTo(22);
    }];
    [self.pigStatusImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_right).offset(5);
        make.centerY.mas_equalTo(self.title.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(42, 16));
    }];
    [self.pigNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_left);
        make.top.mas_equalTo(self.title.mas_bottom).offset(5);
        make.height.mas_equalTo(18);
    }];
    [self.arrearageMoney mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.title.mas_left);
        make.top.mas_equalTo(self.pigNum.mas_bottom).offset(5);
        make.height.mas_equalTo(20);
    }];
    [self.expiredTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_left);
        make.top.mas_equalTo(self.headImg.mas_bottom).offset(16);
        make.height.mas_equalTo(16);
    }];
    [self.address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImg.mas_left);
        make.top.mas_equalTo(self.expiredTime.mas_bottom).offset(10);
        make.height.mas_equalTo(16);
    }];
    [self.lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.address.mas_bottom).offset(8);
        make.height.mas_equalTo(15);
    }];

    [self.lookBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineImg.mas_bottom).offset(11);
        make.centerX.mas_equalTo(self.bgView.mas_centerX);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(100);
    }];
    [self.transferBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.lookBtn.mas_centerY);
        make.centerX.mas_equalTo(self.bgView.mas_centerX).multipliedBy(0.3);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(100);
    }];
    [self.payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.lookBtn.mas_centerY);
        make.centerX.mas_equalTo(self.bgView.mas_centerX).multipliedBy(1.6);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(100);
    }];
    [self.vLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((SCREEN_WIDTH - 30)/3);
        make.size.mas_equalTo(CGSizeMake(1, 12));
        make.centerY.mas_equalTo(self.lookBtn.mas_centerY);
    }];
    [self.vLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-(SCREEN_WIDTH - 30)/3);
        make.size.mas_equalTo(CGSizeMake(1, 12));
        make.centerY.mas_equalTo(self.lookBtn.mas_centerY);
    }];
    [self.unableBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}
- (void)transferTapped {
    if (self.transferBlock) {
        self.transferBlock();
    }
}
- (void)lookTapped {
    if (self.lookBlock) {
        self.lookBlock();
    }
}
- (void)payTapped {
    if (self.payBlock) {
        self.payBlock();
    }
}
- (void)configCellWithModel:(Adopted *)model adoptedType:(AdoptedType)adoptedType {
    self.title.text = model.varieties;
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
    self.pigNum.text = [NSString stringWithFormat:@"仔猪编号:%@",model.pigNum];
    [self.address setTitle:model.tribeName forState:UIControlStateNormal];
    
    NSString *date = [NSString stringWithFormat:@"托管到期:%@(还剩%@天)",model.overTime,model.overDays];
    [self.expiredTime setTitle:date forState:UIControlStateNormal];
    
    
    self.unableBgView.hidden = YES;
    switch ([model.status intValue]) {//
        case PigStatusNormal://正常
            break;
        case PigStatusInvalid://无效
            self.unableBgView.hidden = NO;
            break;
        case PigStatusUnableTransfer://不可转让
            self.transferBtn.enabled = NO;
            break;
        default:
            self.transferBtn.enabled = YES;
            
            break;
    }
    //除饲养仔猪  其他去转让都不可点
    if (adoptedType == AdoptedTypeBuy) {
        self.transferBtn.enabled = YES;

    } else {
        self.transferBtn.enabled = NO;

    }   
    if ([model.tags isEqualToString:@"欠费"]) {
        self.pigStatusImg.image = [UIImage imageNamed:@"adopted_arrearage"];
    } else if ([model.tags isEqualToString:@"快到期"]) {
        self.pigStatusImg.image = [UIImage imageNamed:@"adopted_expired"];
        
    } else if ([model.tags isEqualToString:@"预售中"]) {
        self.pigStatusImg.image = [UIImage imageNamed:@"adopted_sale"];
        
    } else if ([model.tags isEqualToString:@"已弃养"]) {
        self.pigStatusImg.image = [UIImage imageNamed:@"adopted_giveup"];
        
    } else if ([JTool iSNull:model.tags]){
        self.pigStatusImg.hidden = YES;
    }
    if ([JTool iSNull:model.arrears]) {
        self.arrearageMoney.hidden = YES;
    } else {
        NSString *arrearageMoney = [NSString stringWithFormat:@"欠费总额:￥%@",model.arrears];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:arrearageMoney];
        text.yy_color = [UIColor colorWithHexString:@"#8D8D8D"];
        text.yy_font = [UIFont smallFont];
        [text yy_setFont:[UIFont largerFont] range:NSMakeRange(6, arrearageMoney.length - 6)];
        [text yy_setColor:[UIColor colorWithHexString:@"#F25C62"] range:NSMakeRange(5, arrearageMoney.length - 5)];
        self.arrearageMoney.attributedText = text;
    }
}
#pragma mark - getter and setter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.shadowColor = [UIColor colorWithRed:217/255.0 green:226/255.0 blue:233/255.0 alpha:0.5].CGColor;
        _bgView.layer.shadowRadius = 12;
        _bgView.layer.shadowOffset = CGSizeMake(0,0);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.cornerRadius = 8;
    }
    return _bgView;
}
- (UIView *)unableBgView {
    if (!_unableBgView) {
        _unableBgView = [[UIView alloc] init];
        _unableBgView.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF" alpha:0.5];
        _unableBgView.layer.cornerRadius = 8;
    }
    return _unableBgView;
}
- (UIImageView *)headImg {
    if (!_headImg) {
        _headImg = [[UIImageView alloc] init];
        _headImg.backgroundColor = [UIColor grayColor];
    }
    return _headImg;
}
- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = [UIColor textBlackColor];
        _title.font = [UIFont largerFont];
    }
    return _title;
}
- (UIImageView *)pigStatusImg {
    if (!_pigStatusImg) {
        _pigStatusImg = [[UIImageView alloc] init];
        
    }
    return _pigStatusImg;
}
- (UILabel *)pigNum {
    if (!_pigNum) {
        _pigNum = [[UILabel alloc] init];
        _pigNum.textColor = [UIColor colorWithHexString:@"#8D8D8D"];
        _pigNum.font = [UIFont smallFont];
    }
    return _pigNum;
}
- (YYLabel *)arrearageMoney {
    if (!_arrearageMoney) {
        _arrearageMoney = [[YYLabel alloc] init];
    }
    return _arrearageMoney;
}
- (UIButton *)expiredTime {
    if (!_expiredTime) {
        _expiredTime = [UIButton buttonWithType:UIButtonTypeCustom];
        _expiredTime.titleLabel.font = [UIFont smallFont];
        _expiredTime.enabled = NO;
        [_expiredTime setTitleColor:[UIColor colorWithHexString:@"#707070"] forState:UIControlStateNormal];
        [_expiredTime setImage:[UIImage imageNamed:@"adopted_time"] forState:UIControlStateNormal];
        [_expiredTime setTitleEdgeInsets:UIEdgeInsetsMake(0, 4 * [JTool getScreenWidthScale], 0, -4 * [JTool getScreenWidthScale])];
    }
    return _expiredTime;
}
- (UIButton *)address {
    if (!_address) {
        _address = [UIButton buttonWithType:UIButtonTypeCustom];
        _address.enabled = NO;
        [_address setTitleColor:[UIColor colorWithHexString:@"#707070"] forState:UIControlStateNormal];
        _address.titleLabel.font = [UIFont smallFont];
        [_address setImage:[UIImage imageNamed:@"adopted_address"] forState:UIControlStateNormal];
        [_address setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];

    }
    return _address;
}
- (UIImageView *)lineImg {
    if (!_lineImg) {
        _lineImg = [[UIImageView alloc] init];
        _lineImg.image = [UIImage imageNamed:@"adopted_line"];
    }
    return _lineImg;
}
- (UIButton *)lookBtn {
    if (!_lookBtn) {
        _lookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lookBtn setTitle:@"看看他" forState:UIControlStateNormal];
        [_lookBtn setTitleColor:[UIColor colorWithHexString:@"#262626" alpha:0.87] forState:UIControlStateNormal];
        _lookBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_lookBtn setImage:[UIImage imageNamed:@"adopted_look"] forState:UIControlStateNormal];
        [_lookBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, -8)];
        [_lookBtn addTarget:self action:@selector(lookTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _lookBtn;
}
- (UIButton *)transferBtn {
    if (!_transferBtn) {
        _transferBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_transferBtn setTitle:@"去转让" forState:UIControlStateNormal];
        [_transferBtn setTitle:@"去转让" forState:UIControlStateDisabled];
        [_transferBtn setTitleColor:[UIColor colorWithHexString:@"#262626" alpha:0.87] forState:UIControlStateNormal];
        [_transferBtn setTitleColor:[UIColor colorWithHexString:@"#D9E2E9" alpha:0.87] forState:UIControlStateDisabled];
        _transferBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_transferBtn setImage:[UIImage imageNamed:@"adopted_transfer"] forState:UIControlStateNormal];
        [_transferBtn setImage:[UIImage imageNamed:@"adopted_transfer_disable"] forState:UIControlStateDisabled];
        [_transferBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, -8)];
        [_transferBtn addTarget:self action:@selector(transferTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _transferBtn;
}
- (UIButton *)payBtn {
    if (!_payBtn) {
        _payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_payBtn setTitleColor:[UIColor colorWithHexString:@"#262626" alpha:0.87] forState:UIControlStateNormal];
        _payBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_payBtn setImage:[UIImage imageNamed:@"adopted_pay"] forState:UIControlStateNormal];
        [_payBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, -8)];
        [_payBtn addTarget:self action:@selector(payTapped) forControlEvents:UIControlEventTouchUpInside];
        [_payBtn setTitle:@"去支付" forState:UIControlStateNormal];
    }
    return _payBtn;
}
- (UIView *)vLine1 {
    if (_vLine1) {
        _vLine1 = [[UIView alloc] init];
        _vLine1.backgroundColor = [UIColor colorWithHexString:@"#D9E2E9" alpha:1];
    }
    return _vLine1;
}
- (UIView *)vLine2 {
    if (_vLine2) {
        _vLine2 = [[UIView alloc] init];
        _vLine2.backgroundColor = [UIColor colorWithHexString:@"#D9E2E9" alpha:0.5];
    }
    return _vLine2;
}
@end
